package com.softstao.guoyu.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.igexin.sdk.PushManager;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.WechatLogin;
import com.softstao.guoyu.mvp.interactor.LoginInteractor;
import com.softstao.guoyu.mvp.presenter.LoginPresenter;
import com.softstao.guoyu.mvp.viewer.WechatBindingViewer;
import com.softstao.softstaolibrary.library.wechatpay.MD5Util;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/24.
 */

public class WechatBindingActivity extends BaseActivity implements WechatBindingViewer{

    @AIPresenter(presenter = LoginPresenter.class, interactor = LoginInteractor.class)
    LoginPresenter presenter;

    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.psd)
    EditText psd;
    @BindView(R.id.loading)
    RelativeLayout loading;

    @Override
    public int _ContentView() {
        return R.layout.activity_wechat_binding;
    }

    @Override
    public void initView() {
        initTitle("绑定账号");
        titleBar.getDivider().setVisibility(View.GONE);
    }

    @OnClick({R.id.del_btn, R.id.submit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.del_btn:
                mobile.setText("");
                break;
            case R.id.submit_btn:
                if (!check()){
                    return;
                }
                binding();
                break;
        }
    }

    private boolean check(){
        if (TextUtils.isEmpty(mobile.getText())){
            LZToast.getInstance(context).showToast("请输入手机号");
            return false;
        }if (TextUtils.isEmpty(psd.getText())){
            LZToast.getInstance(context).showToast("请输入登录密码");
            return false;
        }
        return true;
    }

    @Override
    public void binding() {
        loading.setVisibility(View.VISIBLE);
        presenter.binding(mobile.getText().toString(), SharePreferenceManager.getInstance().getOpen_id(), MD5Util.MD5Encode(psd.getText().toString(),null));
    }

    @Override
    public void bindingResult(WechatLogin wechatLogin) {
        loading.setVisibility(View.GONE);
        SharePreferenceManager.getInstance().setLogin(true);
        SharePreferenceManager.getInstance().setAgentId(wechatLogin.getAgentId());
        PushManager.getInstance().bindAlias(context,String.valueOf(SharePreferenceManager.getInstance().getAgentId()));
        startActivity(new Intent(context, MainActivity.class).putExtra("index", "4"));
        finish();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
